home *** CD-ROM | disk | FTP | other *** search
/ Total Network Tools 2002 / NextStepPublishing-TotalNetworkTools2002-Win95.iso / Archive / Web Server / PHP.EXE / pear / PHPDoc / renderer / html / PhpdocHTMLClassRenderer.php next >
Encoding:
PHP Script  |  2001-02-18  |  13.4 KB  |  389 lines

  1. <?php
  2. /**
  3. * Renders classes.
  4. *
  5. * @version  $Id: PhpdocHTMLClassRenderer.php,v 1.5 2001/02/18 16:29:21 uw Exp $
  6. */
  7. class PhpdocHTMLClassRenderer extends PhpdocHTMLDocumentRenderer {
  8.  
  9.     /**
  10.     * Internal array of " " strings to format HTML output.
  11.     *
  12.     * @var  array
  13.     */
  14.     var $indent = array();
  15.  
  16.     /**
  17.     * Array of variables found in the xml document.
  18.     *
  19.     * @var  array
  20.     */
  21.     var $variables = array();
  22.  
  23.     /**
  24.     * Sets the xml and template root directory.
  25.     * 
  26.     * @param    string  XML file path
  27.     * @param    string  Template file path
  28.     * @param    string  Name of the current application
  29.     * @param    string  Filename extension
  30.     * @see      setPath(), setTemplateRoot()
  31.     */
  32.     function PhpdocHTMLClassRenderer($path, $templateRoot, $application, $extension = ".html") {
  33.  
  34.         $this->setPath($path);
  35.         $this->setTemplateRoot($templateRoot);
  36.         $this->application = $application;
  37.         $this->file_extension = $extension;
  38.         
  39.         $this->accessor = new PhpdocClassAccessor;
  40.         $this->tpl = new IntegratedTemplate($this->templateRoot);
  41.         $this->fileHandler = new PhpdocFileHandler;
  42.  
  43.     } // end constructor
  44.  
  45.     /**
  46.     * Renders a class.
  47.     *
  48.     * @param    string  XML source file
  49.     * @param    string  Name of the HTML target file.
  50.     * @access   public
  51.     */    
  52.     function renderClass($xmlfile, $htmlfile = "") {
  53.  
  54.         $this->tpl->loadTemplatefile("class.html");    
  55.         if ("" == $htmlfile)
  56.             $htmlfile = substr($xmlfile, 6, -4) . $this->file_extension;
  57.  
  58.         $this->accessor->loadXMLFile($this->path.$xmlfile);
  59.         
  60.         $this->renderSubclasses();
  61.         $this->renderInherited();
  62.         $this->renderFunctions();
  63.         $this->renderVariables();
  64.         $this->renderUses();
  65.         $this->renderConstants();
  66.         
  67.         $class = $this->accessor->getClassdata();
  68.         $tplvars = array();
  69.  
  70.         $tplvars["CLASS_FILE"]      = $class["file"]["value"];
  71.         $tplvars["CLASS_NAME"]      = $class["name"];
  72.         $tplvars["CLASS_ACCESS"]    = $class["access"];
  73.         $tplvars["CLASS_PACKAGE"]   = $class["package"];
  74.  
  75.         if ("" != $class["extends"])
  76.             $tplvars["CLASS_EXTENDS"] = sprintf('extends <a href="%s">%s</a>', 
  77.                                                 $class["extends"].$this->file_extension, 
  78.                                                 $class["extends"]
  79.                                             );
  80.             
  81.         $tplvars["CLASS_UNDOC"]    = ("true" == $class["undoc"]) ? $this->undocumented : "";
  82.         
  83.         $tplvars["CLASS_ABSTRACT"] = ("true" == $class["abstract"]) ? "abstract" : "";
  84.         $tplvars["CLASS_STATIC"]   = ("true" == $class["static"]) ? "static" : "";
  85.         $tplvars["CLASS_FINAL"]    = ("true" == $class["final"]) ? "final" : "";
  86.         
  87.         $tplvars["CLASS_TREE"]     = $this->getClasstree($class["name"]);
  88.         
  89.         if (isset($class["doc"]["link"]))
  90.             $this->renderLinks($class["doc"]["link"], "class_");
  91.             
  92.         if (isset($class["doc"]["author"]))
  93.             $this->renderAuthors($class["doc"]["author"], "class_");
  94.             
  95.         if (isset($class["doc"]["see"]))
  96.             $this->renderSee($class["doc"]["see"], "class_");
  97.         
  98.         $fields = array("version", "deprecated", "copyright", "since", "magic");
  99.         reset($fields);
  100.         while (list($k, $field) = each($fields)) 
  101.             if (isset($class["doc"][$field])) {
  102.                 $this->tpl->setCurrentBlock("class_" . strtolower($field));
  103.                 $this->tpl->setVariable(strtoupper($field), $class["doc"][$field]["value"]);
  104.                 $this->tpl->parseCurrentBlock();
  105.             }
  106.  
  107.         $fields = array( "description", "shortdescription" );
  108.  
  109.         reset($fields);
  110.         while (list($k, $field) = each($fields)) 
  111.             if (isset($class["doc"][$field]))
  112.                 $tplvars["CLASS_".strtoupper($field)] = $this->encode($class["doc"][$field]["value"]);
  113.  
  114.         $this->tpl->setCurrentBlock("__global__");
  115.         $this->tpl->setVariable($tplvars);
  116.         $this->tpl->setVariable("APPNAME", $this->application);
  117.  
  118.         $this->fileHandler->createFile($this->path.$htmlfile, $this->tpl->get() );
  119.         $this->tpl->free();    
  120.  
  121.     } // end func renderClass
  122.  
  123.     /**
  124.     * Renders a list of inherited elements.
  125.     *
  126.     * @see  renderInheritedElements()
  127.     */
  128.     function renderInherited() {
  129.  
  130.         $this->renderInheritedElements(    $this->accessor->getInheritedFunctions(),
  131.                                             "inheritedfunctions",
  132.                                             "function"
  133.                                         );
  134.                                                                     
  135.         $this->renderInheritedElements( $this->accessor->getInheritedVariables(),
  136.                                             "inheritedvariables",
  137.                                             "variable"
  138.                                         );
  139.  
  140.         $this->renderInheritedElements( $this->accessor->getInheritedConstants(),
  141.                                             "inheritedconstants",
  142.                                             "constant"
  143.                                         );
  144.  
  145.         $this->renderInheritedElements(    $this->accessor->getInheritedUses(),
  146.                                             "inheriteduses",
  147.                                             "uses"
  148.                                         );
  149.  
  150.     } // end func renderInherited
  151.  
  152.     /**
  153.     * Renders a list of a certain inherited element.
  154.     *
  155.     * @param    array   List of inherited elements.
  156.     * @param    string  Templateblockname
  157.     * @param    string  Element type: function, variable...
  158.     * @see      renderInherited()
  159.     */
  160.     function renderInheritedElements($inherited, $block, $type) {
  161.         
  162.         if (0 == count($inherited))
  163.             return;
  164.  
  165.         $this->tpl->setCurrentBlock($block);
  166.  
  167.         reset($inherited);
  168.         while (list($source, $elements) = each($inherited)) {
  169.             
  170.             $value = "";
  171.             
  172.             reset($elements);
  173.             while (list($k, $element) = each($elements))
  174.                 $value .= sprintf('<a href="%s#%s_%s">%s</a>, ', 
  175.                                         $source.$this->file_extension,
  176.                                         $type, 
  177.                                         $element, 
  178.                                         $element
  179.                                 );
  180.             $value = substr($value, 0, -2);
  181.             
  182.             $this->tpl->setVariable("SOURCE", $source);
  183.             $this->tpl->setVariable("ELEMENTS", $value);
  184.             $this->tpl->parseCurrentBlock();
  185.         }
  186.  
  187.     } // end func renderInheritedElements
  188.  
  189.     /**
  190.     * Renders a list of direct known subclasses.
  191.     */
  192.     function renderSubclasses() {
  193.         
  194.         $subclasses = $this->accessor->getSubclasses();
  195.         if (0 == count($subclasses)) 
  196.             return;
  197.         
  198.         $elements = "";
  199.         reset($subclasses);
  200.         while (list($k, $subclass) = each($subclasses))
  201.             $elements .= sprintf('<a href="%s">%s</a>, ', $subclass.$this->file_extension, $subclass);
  202.         
  203.         $elements    = substr($elements, 0, -2);
  204.         
  205.         if ("" != $elements) {
  206.         
  207.             $this->tpl->setCurrentBlock("subclasses");
  208.             $this->tpl->setVariable("ELEMENTS", $elements);
  209.             $this->tpl->parseCurrentBlock();
  210.         }
  211.  
  212.     } // end func renderSubclasses
  213.  
  214.     /**
  215.     * Adds a summary and a detailed list of all variables to the template.
  216.     *
  217.     * @see  renderVariableSummary(), renderVariableDetail()
  218.     */
  219.     function renderVariables() {
  220.         
  221.         $this->variables["private"] = $this->accessor->getVariablesByAccess("private");
  222.         $this->variables["public"]     = $this->accessor->getVariablesByAccess("public");
  223.         
  224.         if (0 == count($this->variables["private"]) && 0 == count($this->variables["public"]))
  225.             return;
  226.         
  227.         $this->renderVariableSummary();
  228.         $this->renderVariableDetail();
  229.  
  230.         $this->variables = array();
  231.  
  232.     } // end func renderVariables
  233.  
  234.     /**
  235.     * Adds a summary of all variables to the template.
  236.     * 
  237.     * The function assumes that there is a block named "variablesummary" and
  238.     * within it a block names "variablesummay_loop" in the template.
  239.     *
  240.     * @see  renderVariableDetail()
  241.     */    
  242.     function renderVariableSummary() {
  243.  
  244.         reset($this->accessModifiers);
  245.         while (list($k, $access) = each($this->accessModifiers)) {
  246.  
  247.             if (0 == count($this->variables[$access]))
  248.                 continue;
  249.                 
  250.             $this->tpl->setCurrentBlock("variablesummary_loop");
  251.             
  252.             reset($this->variables[$access]);
  253.             while (list($name, $variable) = each($this->variables[$access])) {
  254.                 
  255.                 $this->tpl->setVariable("NAME", $name);
  256.                 $this->tpl->setVariable("TYPE", $variable["type"]);
  257.                 
  258.                 if (isset($variable["doc"]["shortdescription"]))
  259.                     $this->tpl->setVariable("SHORTDESCRIPTION", $this->encode($variable["doc"]["shortdescription"]["value"]));
  260.             
  261.                 $this->tpl->parseCurrentBlock();                
  262.                 
  263.             }
  264.             
  265.             $this->tpl->setCurrentBlock("variablesummary");
  266.             $this->tpl->setVariable("ACCESS", ucfirst($access));
  267.             $this->tpl->parseCurrentBlock();
  268.             
  269.         }
  270.  
  271.     } // end func renderVariableSummary
  272.  
  273.     /**
  274.     * Adds a detailed list of all variables to the template.
  275.     * 
  276.     * The function assumes that there is a block named "variabledetails"
  277.     * and within it a block names "variablesdetails_loop" in the template.
  278.     *
  279.     * @see  renderVariableSummary()
  280.     */    
  281.     function renderVariableDetail() {
  282.         
  283.         reset($this->accessModifiers);
  284.         while (list($k, $access) = each($this->accessModifiers)) {
  285.  
  286.             if (0 == count($this->variables[$access]))
  287.                 continue;
  288.  
  289.             reset($this->variables[$access]);
  290.             while (list($name, $variable) = each($this->variables[$access])) {
  291.             
  292.                 $tplvars = array();
  293.                 $tplvars["NAME"]    =    $variable["name"];
  294.                 $tplvars["ACCESS"]  = $variable["access"];
  295.                 $tplvars["TYPE"]    = $variable["type"];
  296.                 $tplvars["VALUE"]   = htmlentities($variable["value"]);
  297.  
  298.                 if ("true" == $variable["undoc"]) 
  299.                     $tplvars["UNDOC"] = $this->undocumented;
  300.  
  301.                 if ("true" == $variable["static"])
  302.                     $tplvars["STATIC"] = "static";
  303.  
  304.                 if ("true" == $variable["final"])
  305.                     $tplvars["FINAL"] = "final";
  306.  
  307.                 if (isset($variable["doc"]["shortdescription"]))
  308.                     $tplvars["SHORTDESCRIPTION"] = $this->encode($variable["doc"]["shortdescription"]["value"]);
  309.  
  310.                 if (isset($variable["doc"]["description"]))
  311.                     $tplvars["DESCRIPTION"] = $this->encode($variable["doc"]["description"]["value"]);
  312.  
  313.                 $this->renderCommonDocfields("variabledetails_", $variable);
  314.  
  315.                 $this->tpl->setCurrentBlock("variabledetails_loop");    
  316.                 $this->tpl->setVariable($tplvars);
  317.                 $this->tpl->parseCurrentBlock();    
  318.  
  319.             }
  320.  
  321.             $this->tpl->setCurrentBlock("variabledetails");
  322.             $this->tpl->setVariable("ACCESS", ucfirst($access) );
  323.             $this->tpl->parseCurrentBlock();
  324.  
  325.         }
  326.  
  327.     } // end func renderVariableDetail
  328.  
  329.     /**
  330.     * Returns a html string that shows the class tree.
  331.     *
  332.     * @param    string  name of the current class
  333.     * @return   string  HTML that shows the tree
  334.     */
  335.     function getClasstree($class) {
  336.         
  337.         $path = $this->accessor->getClasstree();
  338.         $level = 0;
  339.         $num = count($path) - 1;
  340.         
  341.         for ($i = $num; $i >= 0; --$i) {
  342.  
  343.             $indent = $this->getIndent($level);
  344.  
  345.             if ($level > 0)
  346.                 $value.= sprintf("%s |<br>%s+-- ", $indent, $indent);
  347.  
  348.             $value.= sprintf('<a href="%s">%s</a><br>', 
  349.                                 $path[$i] . $this->file_extension,
  350.                                 $path[$i]
  351.                             );
  352.             ++$level;
  353.             
  354.         }
  355.  
  356.         $indent = $this->getIndent($level);
  357.  
  358.         if ($level > 0)
  359.             $value.= sprintf("%s |<br>%s+-- ", $indent, $indent);
  360.  
  361.         $value.= sprintf('%s<br>', $class);
  362.  
  363.         return $value;
  364.     } // end func getClasstree
  365.  
  366.     /**
  367.     * Returns a certain number of " "s.
  368.     *
  369.     * @param    int     number of " " required.
  370.     * @see      $indent
  371.     * @return   tring   A string with the requested number of nunbreakable html spaces
  372.     */
  373.     function getIndent($level) {
  374.  
  375.         if (!isset($this->indent[$level])) {
  376.  
  377.             $html = "";
  378.             for ($i = 0; $i < $level; ++$i)
  379.                 $html .= "  ";
  380.  
  381.             $this->indent[$level] = $html;
  382.  
  383.         }
  384.  
  385.         return $this->indent[$level];
  386.     } // end func getIndent
  387.  
  388. } // end class PhpdocHTMLClassRenderer
  389. ?>